package cn.edu.neusoft.maxingxin231.yuejian06.Fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

import cn.edu.neusoft.maxingxin231.yuejian06.Activity.AlterUserinfoActivity;
import cn.edu.neusoft.maxingxin231.yuejian06.Activity.AttListActivity;
import cn.edu.neusoft.maxingxin231.yuejian06.Activity.LoginActivity;
import cn.edu.neusoft.maxingxin231.yuejian06.Activity.OrderListActivity;
import cn.edu.neusoft.maxingxin231.yuejian06.Activity.PostListActivity;
import cn.edu.neusoft.maxingxin231.yuejian06.Bean.UserInforBean;
import cn.edu.neusoft.maxingxin231.yuejian06.Listener.LoginListener;
import cn.edu.neusoft.maxingxin231.yuejian06.Model.AlterUserinfoModel;
import cn.edu.neusoft.maxingxin231.yuejian06.Model.UserModel;
import cn.edu.neusoft.maxingxin231.yuejian06.R;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * A simple {@link Fragment} subclass.

 */
public class MyFragment extends BaseFragment  {
    ImageButton btn_set,btn_more1,btn_more2,btn_more3;
    ImageView img_userpic;
    TextView tv_username;
    Button bt_zx;
    String id,user_name,user_img,user_password;

    LoginListener<UserInforBean> userInforBeanLoginListener = new LoginListener<UserInforBean>() {
        @Override
        public void onResponse(UserInforBean userInforBean) {
            user_img = userInforBean.getUser_pic();
            user_name = userInforBean.getUser_name();
            user_password=userInforBean.getUser_password();
            tv_username.setText(user_name);
           Picasso.with(getActivity()).load("http://10.0.2.2:80/yuejian14/public/static/user_img/"+user_img).into(img_userpic);
//            Glide.with(getActivity()).load("http://10.0.2.2:80/yuejian14/public/static/user_img/"+user_img)
//                    .bitmapTransform(new CropCircleTransformation(getActivity()))
//                    .into(img_userpic);

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getActivity(), "33", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void initView() {
        btn_set = (ImageButton)view.findViewById(R.id.btn_set);
        btn_more1 = (ImageButton)view.findViewById(R.id.btn_more1);
        btn_more2 = (ImageButton)view.findViewById(R.id.btn_more2);
        btn_more3 = (ImageButton)view.findViewById(R.id.btn_more3);
        img_userpic = (ImageView)view.findViewById(R.id.img_userpic);
        tv_username = (TextView)view.findViewById(R.id.tv_username);
        bt_zx =(Button)view.findViewById(R.id.bt_zx);
        id = getUser_id();
        Toast.makeText(getActivity(), id, Toast.LENGTH_SHORT).show();
    }

    @Override
    void initEvent() {
        btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),AlterUserinfoActivity.class);
                intent.putExtra("user_img",user_img);
                intent.putExtra("user_name",user_name);
                intent.putExtra("user_password",user_password);
                startActivity(intent);
            }
        });

        bt_zx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
            }
        });


        //订单
        btn_more3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent  = new Intent(getActivity(),OrderListActivity.class);
                startActivity(intent);
            }
        });
        //帖子
        btn_more2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),PostListActivity.class);
                startActivity(intent);
            }
        });

        //关注
        btn_more1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent  = new Intent(getActivity(),AttListActivity.class);
                startActivity(intent);
            }
        });


    }


    @Override
    void initData() {
        UserModel userModel = new UserModel();
        userModel.getUserInfor(getUser_id(),userInforBeanLoginListener);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setLayout_file(R.layout.fragment_my);
        return super.onCreateView(inflater,container,savedInstanceState);
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
    @Override
    public void onResume() {
        UserModel userModel = new UserModel();
        userModel.getUserInfor(getUser_id(),userInforBeanLoginListener);
        super.onResume();
    }
}
